/**
 * @author 铭
 * @TODO 11/1/2024
 */
package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.authentication.PermissionAuth;
import com.woniuxy.entitys.TbDepartment;
import com.woniuxy.entitys.TbShareholder;
import com.woniuxy.entitys.TbUser;
import com.woniuxy.services.ShareholderService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginUser;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.Map;

@RestController
public class ShareholderController {
    @Autowired
    private ShareholderService shareholderService;
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("company/selAll")
    public ResultVO selAll(@RequestBody Map<String,Object> map){
        Integer pageNum = (Integer) map.get("pageNum");
        String value = (String) map.get("value");
        List<String> dateArr = (List<String>) map.get("dateArr");
        PageInfo<TbShareholder> tbShareholderPageInfo = shareholderService.selAll(value, pageNum,dateArr);
        return ResultVO.success("",tbShareholderPageInfo);
    }
    @PutMapping("company/refDel/{id}")
    @PermissionAuth("dmlShareholder")
    public ResultVO refDel(@PathVariable Integer id){
        int i = shareholderService.refDel(id);
        return i>0?ResultVO.success(""):ResultVO.failure();
    }
    @PostMapping("company/addShare")
    @PermissionAuth("dmlShareholder")
    public ResultVO addShare(@RequestBody TbUser user){
        int i = shareholderService.addShare(user);
        return i>0?ResultVO.success(""):ResultVO.failure();
    }
    @PostMapping("company/updShare")
    @PermissionAuth("dmlShareholder")
    public ResultVO edit(@RequestBody TbShareholder shareholder){

        int i = shareholderService.edit(shareholder);
        return i>0?ResultVO.success(""):ResultVO.failure();
    }
    @GetMapping("/company/getallshareholder")
    public ResultVO getallshareholder(){
        int getallshareholder = shareholderService.getallshareholder();
        return  getallshareholder>=0?ResultVO.success("查询成功",getallshareholder):ResultVO.failure("查询失败");
    }
    @GetMapping("/company/getprofit")
    public ResultVO getprofit(){
        List<Map> getprofit = shareholderService.getprofit();
        return  getprofit!=null?ResultVO.success("查询成功",getprofit):ResultVO.failure("查询失败");
    }
    @GetMapping("/company/shareholderInformation")
    public ResultVO getAllInfo(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String header = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(header);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:" + username);
        Integer uid = loginUser.getUser().getUid();
        TbShareholder shareholder = shareholderService.selShareholderByUid(uid);
        return shareholder!=null?ResultVO.success("success",shareholder):ResultVO.failure("error");
    }
    @PostMapping("/company/shareholderInformation")
    public ResultVO saveShareholder(@RequestBody TbShareholder shareholder){
        int i = shareholderService.updataShareholder(shareholder);
        return i>0?ResultVO.success(""):ResultVO.failure();
    }

}
